#include <iostream>
#include <functional>

using std::cout;
using std::endl;
using std::bind;

int add(int x, int y)
{
    cout << "int add(int, int)" << endl;
    return x + y;
}

int multiply(int x, int y, int z)
{
    cout << "int multiply(int, int, int)" << endl;
    return x * y * z;
}

class Example
{
public:
    //Example * const this
    int add(int x, int y)
    {
        cout << "int Example::add(int, int)" << endl;
        return x + y;
    }
};

#if 0
template <typename T>
int func2(T &&a)
{
    int & &&t = 10;
    int b = 10;
    int & &&t1 = b;
}
#endif

void test()
{
    int a = 7;
    int b = 2;
    /* auto f = bind(&add, 1, 2); */
    auto f = bind(&add, a, b);
    cout << "f() = " << f() << endl;

    cout << endl;
    auto f2 = bind(&multiply, 3, 4, 5);
    cout << "f2() = " << f2() << endl;

    cout << endl;
    Example ex;
    auto f3 = bind(&Example::add, &ex, 10, 20);
    cout << "f3() = " << f3() << endl;

    cout << endl;
    //占位符
    auto f4 = bind(&add, 10, std::placeholders::_1);
    cout << "f4(40) = " << f4(40) << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

